package com.hy.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hy.bean.Follow;
import com.hy.bean.PageBean;
import com.hy.bean.TableLike;
import com.hy.dao.CommentDao;
import com.hy.dao.FollowDao;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

public class CommentController {
    private CommentDao commentDao = new CommentDao();
    private ObjectMapper objectMapper = new ObjectMapper();

    public PageBean<List<Map<String, Object>>> selectCom(HttpServletRequest request, HttpServletResponse response, Integer id) throws Exception {
        List<Map<String, Object>> mapList = commentDao.selectBy(id, 0);
        commentDao.select(mapList,id);
        for(Map<String, Object> map : mapList){
            map.put("create_time", map.get("create_time").toString());
        }

        PageBean<List<Map<String, Object>>> pb = new PageBean();
        if (mapList.size() > 0) {
            pb.setCode(1);
            pb.setMsg("首页数据返回");
            pb.setStatus(500);
            pb.setData(mapList);
            return pb;
        } else {
            return null;
        }
    }

    public PageBean<Boolean> selectComXh(HttpServletRequest request, HttpServletResponse response, Integer uid,Integer cid) throws Exception {
        System.out.printf("uid=%d,cid=%d",uid,cid);
        boolean flag = commentDao.select(uid, cid);
        PageBean<Boolean> pb = new PageBean();
        if (flag) {
            pb.setCode(1);
            pb.setMsg("首页数据返回");
            pb.setStatus(500);
            pb.setData(flag);
            return pb;
        } else {
            pb.setCode(1);
            pb.setMsg("首页数据返回");
            pb.setStatus(500);
            pb.setData(flag);
            return pb;
        }
    }

    public  PageBean<Boolean> updateCom(HttpServletRequest request, HttpServletResponse response, Integer uid,Integer cid) throws Exception {
        TableLike tableLike = new TableLike(uid,cid,"1", uid,null,null,null, "0");
        boolean flag = false;
        boolean flagsel = commentDao.select(uid, cid);
        if(!flagsel){
            flag = commentDao.updateById(cid);
            flag = commentDao.insert(tableLike);
        }else{
            commentDao.updateByTom(tableLike);
        }

        PageBean<Boolean> pb = new PageBean();
        if (flag) {
            pb.setCode(1);
            pb.setMsg("首页数据返回");
            pb.setStatus(500);
            pb.setData(flag);
            return pb;
        } else {
            return null;
        }
    }

    public  PageBean<Boolean> updateCom2(HttpServletRequest request, HttpServletResponse response, Integer uid,Integer cid) throws Exception {
        TableLike tableLike = new TableLike(uid,cid,"1", null,uid,null,null, "1");
        boolean flag = commentDao.updateByTom(tableLike);
                flag = commentDao.updateById2(cid);
        PageBean<Boolean> pb = new PageBean();
        if (flag) {
            pb.setCode(1);
            pb.setMsg("首页数据返回");
            pb.setStatus(500);
            pb.setData(flag);
            return pb;
        } else {
            pb.setCode(1);
            pb.setMsg("首页数据返回");
            pb.setStatus(500);
            pb.setData(flag);
            return pb;
        }
    }


}
